//
//  SMHHomeView.swift
//  SMHCommerce
//
//  Created by lvzhao on 2020/7/1.
//  Copyright © 2020 lvzhao. All rights reserved.
//

import UIKit

class SMHHomeView: LZBaseView ,UICollectionViewDelegate,UICollectionViewDataSource{

    var viewModel = SMHHomeViewModel()
    
    
    override init(viewModel: LZBaseViewModel) {
        super.init(viewModel: viewModel)
        self.viewModel = viewModel as! SMHHomeViewModel
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 创建UI
    func setupUI()  {
        let homeNav = SMHHomeNavView.init(viewModel: self.viewModel)
        self.addSubview(homeNav)
        homeNav.backgroundColor = UIColor.red;
        homeNav.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(kNavHeight)
        }
          
        self.addSubview(self.collectionView)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView .register(SMHHomeProductCell.self, forCellWithReuseIdentifier: "SMHHomeProductCell")
        self.collectionView .register(SMHHomeHeadView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SMHHomeHeadView")
        self.collectionView .register(SMHHomeFootView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "SMHHomeFootView")
        self.collectionView.keyboardDismissMode = .onDrag
            
            
        self.collectionView.snp.makeConstraints { (mark) in
            mark.left.right.equalTo(0)
            mark.bottom.equalTo(0)
            mark.top.equalTo(homeNav.snp.bottom)
        }
        
        self.viewModel.publishSubject.subscribe(onNext: { code in
            print("第1次订阅：", code)
            let type : Int = code as! Int
            switch type {
            case 1:
                self.collectionView .reloadData()
            default:
                print(type)
            }
        }).disposed(by: self.viewModel.disposeBag)
    }

    //MARK:UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.viewModel.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let productCell = collectionView .dequeueReusableCell(withReuseIdentifier: "SMHHomeProductCell", for: indexPath) as! SMHHomeProductCell
        productCell.backgroundColor = UIColorRandom()
        productCell.modelObject = self.viewModel.dataArray[indexPath.row] as AnyObject
        return productCell
        
    }
        
    //MARK:UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:UICollectionReusableView!
        if kind == UICollectionView.elementKindSectionHeader{
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SMHHomeHeadView", for: indexPath) as! SMHHomeHeadView
            let headView = reusableview as! SMHHomeHeadView
            headView.viewModel = self.viewModel
            reusableview.backgroundColor = UIColorRandom()
        } else {
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SMHHomeFootView", for: indexPath) as! SMHHomeFootView
        }
        return reusableview
    }
        
        
        
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
     
        
    }
        
    //MARK: 懒加载
    lazy var collectionView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        //设置item的行间距.列间距
        layout.minimumLineSpacing = 7
        layout.minimumInteritemSpacing = 7

        //设置item的大小
        let invite = 40.0
        let itemW = (Double(kScreenWidth) - invite)/2.0
        layout.itemSize = CGSize(width: itemW, height: 260)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)


        //设置区头 和区尾 大小
        layout.headerReferenceSize = CGSize(width: kScreenWidth, height: 325)
        layout.footerReferenceSize = CGSize(width: kScreenWidth, height: 10)

        layout.scrollDirection = .vertical

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

        return collectionView
    }()

}
